//
//  ComicListCell.m
//  A段项目,漫画
//
//  Created by dllo on 15/4/22.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ComicListCell.h"
#define WIDTH [[UIScreen mainScreen] bounds].size.width
//#define HEIGHT 205
#define IMAGEWIDTH self.moreButton.imageView.image.size.width
#define IMAGEHEIGHT self.moreButton.imageView.image.size.height
@implementation ComicListCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.myData = [NSMutableArray array];
        self.comicListTitle = [[UILabel alloc] init];
        [self.contentView addSubview:self.comicListTitle];
        [self.comicListTitle release];
        
        self.moreButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.contentView addSubview:self.moreButton];
        [self.moreButton setImage:[UIImage imageNamed:@"iconfont-zhankai"] forState:UIControlStateNormal];
        
        //layout
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
        //336  200
        CGFloat bili = 200.0 / 336;
        flow.itemSize = CGSizeMake(WIDTH / 2.2, WIDTH / 2.2 * bili + 30);
        flow.minimumInteritemSpacing = 10;
        flow.minimumLineSpacing = 10;
        flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        //边距
        flow.sectionInset = UIEdgeInsetsMake(5, 15, 5, 10);
        
        self.comicListCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40, WIDTH, WIDTH / 2.2 * bili + 40) collectionViewLayout:flow];
        [self.contentView addSubview:self.comicListCollection];
        self.comicListCollection.delegate = self;
        self.comicListCollection.dataSource = self;
        //注册
        [self.comicListCollection registerClass:[ComicListCollectionCell class] forCellWithReuseIdentifier:@"reuseCollection"];
        self.comicListCollection.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ComicListData *cd = self.myData[indexPath.row];
    [self.delegate getComicListID:cd.comic_id];
}


-(void)layoutSubviews{
    [super layoutSubviews];
    self.comicListTitle.frame = CGRectMake(0, 0, WIDTH, 40);
    self.comicListTitle.backgroundColor = [UIColor colorWithRed:244 / 255.0 green:244 / 255.0 blue:244 / 255.0 alpha:1];
    self.comicListTitle.layer.borderWidth = 5;
    self.comicListTitle.layer.cornerRadius = 10;
    self.comicListTitle.layer.borderColor = [UIColor clearColor].CGColor;
    
    
    self.moreButton.frame = CGRectMake(WIDTH - self.moreButton.imageView.image.size.width - 5, 5, self.moreButton.imageView.image.size.width, self.moreButton.imageView.image.size.height);
    [self.moreButton addTarget:self action:@selector(moreButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)moreButtonAction:(UIButton *)button{
    self.myMoreBlock(self.comic_list_id);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.comicListCount;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ComicListCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseCollection" forIndexPath:indexPath];
    
    
    ComicListData *cd = self.myData[indexPath.row];
    cell.imageLabel.text = cd.title;
    NSURL *url = [NSURL URLWithString:cd.cover_url];
    UIImage *placeImage = [UIImage imageNamed:@"FOR@{XC4(JCI7}{P@Y]({{1－1（被拖移）.tiff"];
    [cell.comicListImage sd_setImageWithURL:url placeholderImage:placeImage];
    return cell;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
